/**
 * @author Austin Andrews (Templarian)
 * @authorurl http://templarian.com
 * @framework DD Framework
 * @licence New BSD License
 * @licenceurl http://ddframework.googlecode.com/svn/trunk/license.txt
 * @overview The http.latency module contains a function for getting an array 
 * of millisecond pings given a url with optional post and get data.
 * request object.
 */
dd.http.latency.extend({
    init:function(){},
    _times:[],
    _numberOfTests:10,
    _test:0,
    /**
     * Use as a function to test a connections ping in milliseconds
     * @namespace dd.http.latency
     * @function
     *  Use to pass data to the server and recieve a value in JSON, XML, or plain text.
     *  @param obj {object} Contains the get, post, url variables and onSuccess (this.results[i] value of -1 denotes failure) Callbacks.
     *  "Request Function"[javascript]@@
     *  dd.http.request({
     *      url:"",                 // URL
     *      get:{},                 // Get Parameters
     *      post:{},                // Post Parameters
     *      onSuccess:function(){}  // onSuccess this.results[], this.average, this.total in milliseconds.
     *  }, totalNumberOfTests);     // defaults to 10 if not defined.
     *  @@
     */
    func:function(requestObject, numberOfTests)
    {
        dd.http.latency._test = 0;
        dd.http.latency._times = [];
        dd.http.latency._numberOfTests = numberOfTests;
        var req = {
            post:requestObject.post,
            get:requestObject.get,
            url:requestObject.url,
            time:true,
            onSuccess:function()
            {
                dd.http.latency._times.push(this.time);
                dd.http.latency._test++;
                if(dd.http.latency._test == dd.http.latency._numberOfTests)
                {
                    var total = dd.http.latency._times;
                    for(var i in dd.http.latency._times)
                        dd.http.latency._times
                    requestObject.onSuccess.call({
                        results:dd.http.latency._times,
                        average:total / dd.http.latency._numberOfTests,
                        total:total
                        });
                }
            }
        };
        for(var i = 1; i <= numberOfTests; i++)
        {
            dd.http.request(req);
        }
    }
});